//
//  CRMessageModel.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/8/13.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "CRMessageModel.h"
#import "CRKit.h"
#import "CRSessionCellLayoutProtocol.h"

@interface CRMessageModel()

@property (nonatomic,strong) NSMutableDictionary *contentSizeInfo;
@property (nonatomic,strong) NSMutableDictionary *replyContentSizeInfo;

@end

@implementation CRMessageModel

@synthesize contentViewInsets  = _contentViewInsets;
@synthesize bubbleViewInsets   = _bubbleViewInsets;
@synthesize replyContentViewInsets  = _replyContentViewInsets;
@synthesize replyBubbleViewInsets   = _replyBubbleViewInsets;
@synthesize shouldShowAvatar   = _shouldShowAvatar;
@synthesize shouldShowNickName = _shouldShowNickName;
@synthesize shouldShowLeft     = _shouldShowLeft;
@synthesize avatarMargin       = _avatarMargin;
@synthesize nickNameMargin     = _nickNameMargin;
@synthesize avatarSize         = _avatarSize;
@synthesize repliedMessage     = _repliedMessage;
@synthesize parentMessage      = _parentMessage;

- (instancetype)initWithMessage:(CRMessage*)message {
    
    if (self = [self init]) {
        
        _message = message;
        _messageTime = message.timestamp;
        _contentSizeInfo = [[NSMutableDictionary alloc] init];
        _replyContentSizeInfo = [NSMutableDictionary dictionary];
        _enableRepliedContent = YES;
        _shouldShowPinContent = YES;
        _enableSubMessages = YES;
    }
    return self;
}

- (void)cleanCache {
    
    [_contentSizeInfo removeAllObjects];
    _contentViewInsets = UIEdgeInsetsZero;
    _bubbleViewInsets = UIEdgeInsetsZero;
    _replyContentViewInsets = UIEdgeInsetsZero;
    _replyBubbleViewInsets = UIEdgeInsetsZero;
}

- (NSString*)description {
    
    return self.message.text;
}

- (BOOL)isEqual:(id)object {
    
    if (![object isKindOfClass:[CRMessageModel class]]) {
        return NO;
    } else {
        CRMessageModel *model = object;
        return [self.message isEqual:model.message];
    }
}

- (CGSize)contentSize:(CGFloat)width {
    
    CGSize size = [self.contentSizeInfo[@(width)] CGSizeValue];
    if (CGSizeEqualToSize(size, CGSizeZero)) {
        [self updateLayoutConfig];
        id<CRSessionCellLayoutProtocol> layoutConfig;//cxx_test = [[CRKit sharedKit] layoutConfig];
        size = [layoutConfig contentSize:self cellWidth:width];
        [self.contentSizeInfo setObject:[NSValue valueWithCGSize:size] forKey:@(width)];
    }
    return size;
}

- (UIEdgeInsets)contentViewInsets {
    
    if (UIEdgeInsetsEqualToEdgeInsets(_contentViewInsets, UIEdgeInsetsZero)) {
        id<CRSessionCellLayoutProtocol> layoutConfig;//cxx_test = [[CRKit sharedKit] layoutConfig];
        _contentViewInsets = [layoutConfig contentViewInsets:self];
    }
    return _contentViewInsets;
}

- (UIEdgeInsets)bubbleViewInsets {
    
    if (UIEdgeInsetsEqualToEdgeInsets(_bubbleViewInsets, UIEdgeInsetsZero)) {
        id<CRSessionCellLayoutProtocol> layoutConfig;//cxx_test = [[CRKit sharedKit] layoutConfig];
        _bubbleViewInsets = [layoutConfig cellInsets:self];
    }
    return _bubbleViewInsets;
}

- (CGSize)replyContentSize:(CGFloat)width {
    
    id<CRSessionCellLayoutProtocol> layoutConfig;//cxx_test = [[CRKit sharedKit] layoutConfig];
    CGSize size = [layoutConfig replyContentSize:self cellWidth:width];
    return size;
}

- (UIEdgeInsets)replyContentViewInsets {
    
    if (UIEdgeInsetsEqualToEdgeInsets(_replyContentViewInsets, UIEdgeInsetsZero)) {
        id<CRSessionCellLayoutProtocol> layoutConfig;//cxx_test = [[CRKit sharedKit] layoutConfig];
        _replyContentViewInsets = [layoutConfig replyContentViewInsets:self];
    }
    return _replyContentViewInsets;
}

- (UIEdgeInsets)replyBubbleViewInsets {
    
    if (UIEdgeInsetsEqualToEdgeInsets(_replyBubbleViewInsets, UIEdgeInsetsZero)) {
        id<CRSessionCellLayoutProtocol> layoutConfig;//cxx_test = [[CRKit sharedKit] layoutConfig];
        _replyBubbleViewInsets = [layoutConfig replyCellInsets:self];
    }
    return _replyBubbleViewInsets;
}


- (void)updateLayoutConfig {
    
    id<CRCellLayoutConfig> layoutConfig = [[CRKit sharedKit] layoutConfig];
    
    _shouldShowAvatar       = [layoutConfig shouldShowAvatar:self];
    _shouldShowNickName     = _focreShowNickName ? YES : [layoutConfig shouldShowNickName:self];
    _shouldShowLeft         = _focreShowLeft ? YES : [layoutConfig shouldShowLeft:self];
    _avatarMargin           = [layoutConfig avatarMargin:self];
    _nickNameMargin         = [layoutConfig nickNameMargin:self];
    _avatarSize             = [layoutConfig avatarSize:self];
}


- (BOOL)shouldShowReadLabel {
    
    if (self.message.session.sessionType == CRSessionTypeP2P) {
        return _shouldShowReadLabel && self.message.isRemoteRead;
    } else {
        return _shouldShowReadLabel;
    }
    
}

- (BOOL)needShowEmoticonsView {
    
    return !CGSizeEqualToSize(CGSizeZero, self.emoticonsContainerSize);
}

@end
